package com.rt.easyjava.base.biz1.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptDataOption;
import com.rt.easyjava.base.biz1.dto.TmptDataOptionDto;
import com.rt.easyjava.base.biz1.mapper.TmptDataOptionMapper;
import com.rt.easyjava.base.biz1.service.intf.TmptDataOptionService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-05-01
 */
@Service(value = "tmptDataOptionService")
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class TmptDataOptionServiceImpl extends ServiceImpl<TmptDataOptionMapper, TmptDataOption> implements TmptDataOptionService {
    private final Logger logger = LoggerFactory.getLogger(TmptDataOptionServiceImpl.class);

    @Autowired
    private MyMapper mapper;
    @Resource
    private TmptDataOptionMapper tmptDataOptionMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptDataOptionDto
    * @return
    */
    @Override
    public Result<TmptDataOptionDto> create(TmptDataOptionDto tmptDataOptionDto) {
        Result<TmptDataOptionDto> result = new Result<>();

        // dto to entity
        TmptDataOption tmptDataOption = mapper.map(tmptDataOptionDto, TmptDataOption.class);

        tmptDataOptionMapper.insert(tmptDataOption);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptDataOptionDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptDataOptionDto> update(TmptDataOptionDto tmptDataOptionDto, Wrapper updateWrapper) {
        Result<TmptDataOptionDto> result = new Result<>();

        // dto to entity
        TmptDataOption tmptDataOption = mapper.map(tmptDataOptionDto, TmptDataOption.class);

        tmptDataOptionMapper.update(tmptDataOption, updateWrapper);

        return result.success();
    }

    /**
    * 分页
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptDataOptionDto> page(Page<TmptDataOption> page, Wrapper<TmptDataOption> queryWrapper){
        Result<TmptDataOptionDto> result = new Result<>();

        // mapper
        IPage<TmptDataOption> iPage = tmptDataOptionMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptDataOptionDto> tmptDataOptionDtoList= mapper.maplist(iPage.getRecords(), TmptDataOptionDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptDataOptionDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptDataOptionDto> selectAll(Wrapper<TmptDataOption> queryWrapper) {
        Result<TmptDataOptionDto> result = new Result<>();

        List<TmptDataOption> list = tmptDataOptionMapper.selectList(queryWrapper);

        List<TmptDataOptionDto> dtoList = mapper.maplist(list, TmptDataOptionDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptDataOptionDto> selectById(Long id) {
        Result<TmptDataOptionDto> result = new Result<>();

        TmptDataOption tmptDataOption = tmptDataOptionMapper.selectById(id);

        TmptDataOptionDto tmptDataOptiondto = mapper.map(tmptDataOption, TmptDataOptionDto.class);

        return result.setObj(tmptDataOptiondto).success();
    }




    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptDataOptionDto> myPage(Page<TmptDataOption> page, Wrapper<TmptDataOption> queryWrapper) {
        Result<TmptDataOptionDto> result = new Result();

        IPage<TmptDataOption> iPage = tmptDataOptionMapper.myPage(page, queryWrapper);

        //查询结果转换成DTO
        List<TmptDataOptionDto> tmptDataOptionDtoList = mapper.maplist(iPage.getRecords(), TmptDataOptionDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptDataOptionDtoList);

        return result.success();
    }

    @Override
    public Result<TmptDataOptionDto> mySelectBy(Wrapper<TmptDataOption> queryWrapper) {
        Result<TmptDataOptionDto> result = new Result();

        List<TmptDataOption> list = tmptDataOptionMapper.mySelectBy(queryWrapper);
        List<TmptDataOptionDto> dtoList = mapper.maplist(list, TmptDataOptionDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptDataOptionDto> mySelectById(long id) {
        Result<TmptDataOptionDto> result = new Result();

        TmptDataOption tmptDataOption = tmptDataOptionMapper.mySelectById(id);
        TmptDataOptionDto dto = mapper.map(tmptDataOption,TmptDataOptionDto.class);
        result.setObj(dto);

        return result.success();
    }



}
